package office.service.business.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import office.action.business.form.SupplierConditionForm;
import office.action.business.form.SupplierForm;
import office.action.business.wrapper.SupplierWrapper;
import office.bo.business.impl.Supplier;
import office.domainset.BizDomainProviderSetImpl;
import office.service.business.ISupplierService;
import platform.common.bo.Page;
import platform.common.dao.IDomainProvider;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public class SupplierServiceImpl extends BaseBizServiceImpl implements ISupplierService{
	
	public void editSupplier(SupplierForm form)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Supplier bo = (Supplier)domainProviderSetImpl.getSupplierDP().newEmptyObj();
			bo =(Supplier) form.copyPropertiesToBO(bo);
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑供应商失败!", e);
		}
	}
	
	public Page getPageInfo(SupplierConditionForm condition)throws BusinessException{
		Page page = new Page();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getSupplierDP();
			int pageSize = Integer.parseInt(Util.parsePropertiesReturnStr("project", "Supplier.pageSize"));
			page.setPageSize(pageSize);
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getFactoryName())){
				condMap.put("factoryName", condition.getFactoryName());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider, "findSupplierByIdAndName", condMap,new HashMap());
			if(list.size()%pageSize == 0){
				page.setTotalPage(list.size()/pageSize);
			}else{
				page.setTotalPage(list.size()/pageSize+1);
			}
			
		}catch(Exception e){
			log.error("", e);
			this.handleBusinessServiceException("获得分页信息失败", e);
		}	
		return page;
	}
	
	public List<SupplierWrapper> findAllSupplier(SupplierConditionForm condition,Page page)throws BusinessException{
		List<SupplierWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getSupplierDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getFactoryName())){
				condMap.put("factoryName", condition.getFactoryName());
			}
			List list=new ArrayList<Supplier>();
			if(null==page){
				list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findSupplierByIdAndName",condMap,new HashMap());
			}else{
				list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findSupplierByIdAndName",condMap,new HashMap(),page);
			}
			SupplierWrapper wrapper;
			for(int i=0;i<list.size();i++){
				Supplier bo = (Supplier)list.get(i);
				wrapper = new SupplierWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取供应商列表失败！", e);
		}
		return wrapperList;
	}

	public void delSupplierById(String id)throws BusinessException{
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getSupplierDP();
			domainProviderSetImpl.getDao().deleteById(provider, id);
		}catch(Exception e){
			log.error("", e);
			throw this.handleBusinessServiceException("删除供应商失败！", e);
		}
	}
	
	public SupplierForm getById(String id)throws BusinessException{
		SupplierForm form = new SupplierForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Supplier bo = (Supplier)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getSupplierDP(), id);
			form.copyPropertiesFromBO(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取供应商失败!", e);
		}
		return form;
	}
}
